<template>
  <div class="boxs">
    <div class="left-content">
      <div v-if="loading" class="loading">加载中...</div>

      <div v-else-if="error" class="error">
        <p class="error-message">{{ error }}</p>
        <el-button type="primary" @click="fetchArticle">重新加载</el-button>
      </div>


      <div v-else>
        <div
          class="article-content"
          v-if="detail && Object.keys(detail).length > 0"
        >
          <el-breadcrumb separator="/" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/about/index' }"
              >首页</el-breadcrumb-item
            >
            <el-breadcrumb-item>{{
              detail.title || "正文"
            }}</el-breadcrumb-item>
          </el-breadcrumb>

          <div class="article-card">
            <h1 class="title">{{ detail.title || "无标题" }}</h1>
            <div class="meta">
              <span>发表于 {{ detail.updateTime || "未知时间" }}</span>
              <span>分类于 {{ detail.categoryName || "未知分类" }}</span>
              <span>阅读量 {{ detail.readNum || 0 }}</span>
            </div>
            <div
              class="content"
              v-html="detail.content || '<p>暂无内容</p>'"
            ></div>
            <div class="tags">
              <el-tag
                type="success"
                size="small"
                v-for="(tag, index) in detail.tags || []"
                :key="index"
                >#{{ tag.name || "未知标签" }}</el-tag
              >
            </div>
          </div>
          <div class="article-nav">
            <div v-if="detail.preArticle" class="prev">
              <router-link :to="`/about/article/${detail.preArticle.id}`">
                <span>上一篇</span>
                <p>{{ detail.preArticle.title || "无" }}</p>
              </router-link>
            </div>
            <div v-if="detail.nextArticle" class="next">
              <router-link :to="`/about/article/${detail.nextArticle.id}`">
                <span>下一篇</span>
                <p>{{ detail.nextArticle.title || "无" }}</p>
              </router-link>
            </div>
          </div>
        </div>

        <div v-else class="no-data">
          <p>暂无文章数据</p>
        </div>
      </div>
    </div>
    <div class="right-panel">
      <Title-view></Title-view>
      <Care-view></Care-view>
      <Tage-view></Tage-view>
    </div>
  </div>
</template>

<script>
import TitleView from "../components/TitleView.vue";
import TageView from "../components/TageView.vue";
import CareView from "../components/CareView.vue";

export default {
  components: {
    TitleView,
    TageView,
    CareView,
  },
  data() {
    return {
      detail: null,
      loading: true,
      error: null,
    };
  },
  created() {
    this.fetchArticle();
  },
  watch: {
    "$route.params.id": {
      handler() {
        this.fetchArticle();
      },
    },
  },
  methods: {
    async fetchArticle() {
      this.loading = true;
      this.error = null;
      this.detail = null;

      try {
        const articleId = this.$route.params.id;
        console.log("当前文章ID", articleId);
        if (!articleId) {
          throw new Error(`无效的文章ID ${articleId}`);
        }

        const res = await this.$axios.post("/api/article/detail",{articleId:articleId});
        console.log("接口返回数据：", res.data);

        if (!res.data) {
          throw new Error("接口无返回数据");
        }
        if (res.data.success) {
          if (res.data.data) {
            this.detail = res.data.data;
          } else {
            throw new Error("文章不存在或已被删除");
          }
        } else {
          throw new Error(res.data.message || "获取文章失败");
        }
      } catch (err) {
        console.error("详情页错误：", err);
        this.error = err.message || "获取文章失败，请稍后重试";
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
/* 外层flex容器：左文章 + 右组件 */
.boxs {
  width: 100%;
  max-width: 1200px; /* 限制最大宽度，优化阅读体验 */
  margin: 0 auto;
  display: flex;
  gap: 20px; /* 左右间距 */
  padding: 20px;
  box-sizing: border-box;
}

/* 左侧内容区：文章主体，占大部分宽度 */
.left-content {
  flex-grow: 1; /* 自适应剩余宽度（主要空间） */
  min-width: 0; /* 避免内容溢出 */
  box-sizing: border-box;
}

/* 右侧面板：标签等组件，固定宽度 */
.right-panel {
  width: 285px;
  flex-shrink: 0; /* 固定宽度，不随屏幕缩小变窄 */
  padding: 15px;
  box-sizing: border-box;
}

/* 加载/错误/无数据状态：在左侧区域居中 */
.loading,
.error,
.no-data {
  text-align: center;
  padding: 50px 0;
  font-size: 16px;
}

.error-message {
  color: #f56c6c;
  margin-bottom: 15px;
}

/* 文章内容样式 */
.breadcrumb {
  margin: 0 0 20px 0;
  font-size: 14px;
}

.article-card {
  background: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.title {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 20px;
  text-align: center;
}

.meta {
  display: flex;
  gap: 20px;
  color: #666;
  font-size: 14px;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
  flex-wrap: wrap; /* 小屏幕自动换行 */
}

.content {
  line-height: 1.8;
  margin: 20px 0;
  color: #333;
  font-size: 16px;
}

.content p {
  margin-bottom: 15px;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 20px 0;
}

/* 上下篇导航 */
.article-nav {
  display: flex;
  justify-content: space-between;
  margin: 30px 0;
  padding: 20px 0;
  border-top: 1px solid #f0f0f0;
}

.article-nav a {
  color: #409eff;
  text-decoration: none;
}

.article-nav a:hover {
  color: #66b1ff;
}

.article-nav p {
  margin-top: 6px;
  font-size: 14px;
}

.prev {
  text-align: left;
}

.next {
  text-align: right;
}

/* 响应式适配：小屏幕垂直堆叠（文章在上，组件在下） */
@media (max-width: 768px) {
  .boxs {
    flex-direction: column;
  }
  .right-panel {
    width: 100%; /* 占满宽度 */
    margin-top: 20px; /* 与上方文章区隔开 */
  }
}
</style>